package com.zzptc.mzb.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzptc.mzb.myapplication.entity.ResultsBean;
import com.zzptc.mzb.myapplication.entity.Shipu;
import com.zzptc.mzb.myapplication.service.MyAppService;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static retrofit2.converter.gson.GsonConverterFactory.*;

public class MainActivity extends AppCompatActivity {
   List<ResultsBean>aaa=new ArrayList<>();
    private static final String TAG = "MainActivity";
    Retrofit retrofit=new  Retrofit.Builder()
            .baseUrl("https://liyuankun.cn")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
             ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getShipu();
        quShuju();
        listView=findViewById(R.id.va);
        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);

    }

    private void quShuju() {
       aaa = LitePal.findAll(ResultsBean.class);
    }

    private void getShipu() {
        //构造接口对象
        MyAppService myAppService=retrofit.create(MyAppService.class);
        //调用接口中的方法 返回Call类型
        Call<Shipu>allShipu=myAppService.allShipu(5);
        allShipu.enqueue(new Callback<Shipu>() {
            @Override
            public void onResponse(Call<Shipu> call, Response<Shipu> response) {
                Shipu body = response.body();
                Log.d(TAG, "onResponse: "+body.getResults().size());
                //将数据放入sqlite数据库中去  使用litepai数据库框架
                List<ResultsBean> results = body.getResults();
                for (ResultsBean r:results){
                  r.save();
                }
            }
            @Override
            public void onFailure(Call<Shipu> call, Throwable t) {
            }
        });
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return aaa.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v=View.inflate(MainActivity.this,R.layout.shipu_item,null);
            ImageView iv_pic=v.findViewById(R.id.va_pic);
            TextView tv_titel=v.findViewById(R.id.va_title);
            //赋值
            ResultsBean resultsBean=aaa.get(position);
            //将值放到控制上显示
            String cover=resultsBean.getCover();//团片的地址
            Glide.with(MainActivity.this)
                    .load(cover)
                    .into(iv_pic);
            tv_titel.setText(resultsBean.getTitle());
            return v;
        }
    }
}
